package com.garygyp.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.garygyp.commonutils.R;
import com.garygyp.staservice.client.UcenterClient;
import com.garygyp.staservice.entity.StatisticsDaily;
import com.garygyp.staservice.mapper.StatisticsDailyMapper;
import com.garygyp.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author GaryGyp
 * @since 2022-05-21
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    // 统计某一天注册人数，生成统计数据
    @Override
    public void registerCount(String day) {
        // 添加记录之前先删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        // 远程调用得到某一天的注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        // 把获取的数据添加到数据库
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(countRegister);//注册人数
        daily.setCourseNum(RandomUtils.nextInt(100, 200));
        daily.setLoginNum(RandomUtils.nextInt(200, 300));//登录数
        daily.setVideoViewNum(RandomUtils.nextInt(200, 300));//视频流量数
        daily.setDateCalculated(day);//统计日期

        //添加到数据库中
        baseMapper.insert(daily);
    }

    // 图表显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        // 根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);

        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        // 因为返回的有两部分数据，日期和日期对应的数量
        // 前端要求数组json结构
        // 后端list集合对应着前端json数组结构
        // 创建两个list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        // 遍历查询所有数据的list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            // 封装日期list集合
            date_calculatedList.add(daily.getDateCalculated());
            // 封装数量集合
            if ("register_num".equals(type)){
                numDataList.add(daily.getRegisterNum());
            }
            if ("login_num".equals(type)){
                numDataList.add(daily.getLoginNum());
            }
            if ("video_view_num".equals(type)){
                numDataList.add(daily.getVideoViewNum());
            }
            if ("course_num".equals(type)){
                numDataList.add(daily.getCourseNum());
            }
        }
        // 把封装后的两个list集合封装到map中返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", date_calculatedList);
        map.put("numDataList", numDataList);

        return map;
    }
}
